package com.example.miaosha.mapper;


import com.example.miaosha.entity.ItemStock;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * Created by lerry_li on 2020/10/20
 */
@Mapper
public interface ItemStockMapper {
    //根据itemId查找库存
    @Select("SELECT * FROM item_stock WHERE item_id = #{id}")
    ItemStock selectByItemId(Integer id);

    //插入一条记录
    @Insert("INSERT INTO item_stock (item_id,stock)" +
            "VALUES (#{itemId},#{stock})")
    void insert(ItemStock itemStock);

    //更新库存
    @Update("UPDATE item_stock SET stock = stock - #{amount} " +
            "WHERE item_id = #{itemId} AND stock >= #{amount}")
    int decreaseStock(Integer itemId,Integer amount);
}
